<?php
namespace Home\Controller;
use Think\Controller;
class JiehuanshuController extends Controller {
      public function jieshu() {          
             $id = $_POST['jie'];
             $userid = substr("$id",-(strlen($id)-6));             
              $_SESSION['jie'] = $userid;
             $data['userid'] = $_SESSION['jie'];
             $data['zhuangtai'] = 1;
             $bookid = M('jieshu')->field('bookid,username')->where($data)->select();
             
             if(!$bookid){
                $this->error('该同学还没有支付书籍押金或者借书编码输入错误！！');
                exit;  
             }
             $ids = array();
         foreach($bookid as $k => $v) {   //把二维数组转化为一维数组
             $ids[] = $v['bookid'];
         }          
           $map['id'] = array('in',$ids);
           $jieBook = M('addbook')->where($map)->select();   
            $this->assign('Book', $jieBook);
            $this->assign('name', $bookid['0']['username']);
            $this->display();         
        }  
	    
	      public function jieshu2() {
	      	   $data['userid'] = $_SESSION['jie'];
			       $data['zhuangtai'] = 1; 
				   $bookid = M('jieshu')->field('bookid,username')->where($data)->find();
				   $map['id'] = array('in',$bookid);
				   $jieBook = M('addbook')->where($map)->select(); 
				    $this->assign('Book', $jieBook);
            $this->assign('name', $bookid['0']['username']);
            $this->display('Jiehuanshu/jieshu'); 
	      }
    
         public function huanshu() {
             $uwe = $_POST['huan'];
             $userid = substr("$uwe",-(strlen($uwe)-6));             
             $_SESSION['huan'] = $userid;
             $data['userid'] = $_SESSION['huan'];
             $data['zhuangtai'] = 1;
             $bookid = M('jieshu')->field('bookid,username')->where($data)->select();
             if(!$bookid){
                $this->error('该同学没有需要还的书籍或者还书编码输入错误！！',U('Jiehuanshu/huanshuview'));
                exit;
             }

             $ids = array();
         foreach($bookid as $k => $v) {   //把二维数组转化为一维数组
             $ids[] = $v['bookid'];
         }          
            $map['id'] = array('in',$ids);
            $jieBook = M('addbook')->where($map)->select();   
            $this->assign('Book', $jieBook);
             $this->assign('name', $bookid['0']['username']);
            $this->display();     
          }    

          public function huanshu2(){
             $data['userid'] = $_SESSION['huan'];
             $data['zhuangtai'] = 1;
             $bookid = M('jieshu')->field('bookid')->where($data)->select();
             if(!$bookid){
                $this->error('该同学没有需要还的书籍或者还书编码输入错误！！',U('Jiehuanshu/huanshuview'));
                exit;
             }

             $ids = array();
         foreach($bookid as $k => $v) {   //把二维数组转化为一维数组
             $ids[] = $v['bookid'];
         }          
            $map['id'] = array('in',$ids);
            $jieBook = M('addbook')->where($map)->select();   
            $this->assign('Book', $jieBook);
            $this->display('Jiehuanshu/huanshu');
          }
            public function bookruku(){
                $bookid = $_GET['id'];
                $data1['id'] = $bookid;
               $money=M('addbook')->where($data1)->field('price')->find();
                $data['id'] = $_SESSION['huan'];                
                 $usermoney=M('user')->where($data)->field('yue')->find();              
                 $xin=$usermoney['yue']+$money['price'];
                 $data3=array('yue'=>$xin);
                 $data['id'] = $_SESSION['huan'];
                 $value=M('user')->where($data)->save($data3);

                $bookid = $_GET['id'];
                $ruku = M('jieshu');               
                $data4['bookid'] = $bookid;                 
                $rukubook = $ruku->where($data4)->delete();                 
                if($rukubook && $value){
                  $this->success('图书入库成功',U('Jiehuanshu/huanshu2'));
                } else {
                  $this->error('图书入库失败,请管理员重新操作');
                }                 
      }

          public function jieshuview(){
             if($_SESSION['id'] && !$_SESSION['adminid']){
                $this->error("若您是管理员，请登录后再扫描", U('Jiehuanshu/jieshuma'));
             }elseif(!$_SESSION['adminid']){
                 $this->error("请管理员登陆后再用微信扫描同学出示的二维码", U('Jiehuanshu/jieshuma')); 
             }elseif($_SESSION['adminid']) {
                 $this->display();  
             } else {
                $this->error("请管理员用微信扫码谢谢"); 
             }
              
        }

        Public function huanshuview(){
            if($_SESSION['id'] && !$_SESSION['adminid']){
                $this->error("若您是管理员，请登录后再扫描", U('Jiehuanshu/jieshuma'));
             }elseif(!$_SESSION['adminid']){
                 $this->error("请管理员登陆后再用微信扫描同学出示的二维码", U('Jiehuanshu/jieshuma')); 
             }elseif($_SESSION['adminid']) {
                 $this->display();  
             } else {
                $this->error("请管理员用微信扫码谢谢"); 
             }
        }
          
        public function jieshuma(){
          if($_SESSION['id']){
              $this->assign('time',rand(100,999));
              $this->assign('shu',rand(100,999));    
             $this->display();
           }else{
          $this->error("您还未登录！", U('My/my'));
         }                   
        }

        public function huanshuma() {
           if($_SESSION['id']){             
              $this->assign('time',rand(100,999));
              $this->assign('shu',rand(100,999));
              $this->display();
         }else{
        $this->error("您还未登录！", U('My/my'));
          }
        }



}